package com.baidu.che.codrivercustom.util;

public class JavaThread {
    private int j;

    public static void main(String[] args) {
        JavaThread jth = new JavaThread();
        AddJ addj = jth.new AddJ();
        DecJ decj = jth.new DecJ();

        for (int i = 0; i < 2; ++i) {
            Thread th = new Thread(addj);
            th.start();
            th = new Thread(decj);
            th.start();
        }
    }

    private synchronized void addJ() {
        j++;
        System.out.println(Thread.currentThread().getName() + " ++J：" + j);
    }

    private synchronized void decJ() {
        j--;
        System.out.println(Thread.currentThread().getName() + " --J：" + j);
    }

    class AddJ implements Runnable {
        public void run() {
            for (int i = 0; i < 100; ++i) {
                addJ();
            }
        }
    }

    class DecJ implements Runnable {
        public void run() {
            for (int i = 0; i < 100; ++i) {
                decJ();
            }
        }
    }
}
